
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/delay.h>
 
#define major_version  200
#define driver1_name "driver1"

static char read_buf[100] = "Hello driver1";
static char write_buf[100];

static int driver1_open(struct inode *inode,struct file *file){
	printk("driver1 open\r\n");
	return 0;
}

static int driver1_close(struct inode *inode,struct file *file){
	printk("driver1 close\r\n");
	return 0;
}



static ssize_t driver1_read(struct file *file, char __user * buf, size_t count, loff_t *ppos){
    printk("driver1 read\r\n");
    copy_to_user(buf,read_buf,count);
    return 0;
}

static ssize_t driver1_write(struct file *file, const char __user * buf, size_t count, loff_t *ppos){
    
    int ret = 0;
    
    printk("driver1 write\r\n");
    
    ret = copy_from_user(write_buf,buf,count);
    if(!ret){
    	printk("receive data: %s \r\n",write_buf);
    }
    
    return 0;
}

 
static struct file_operations ops={
	.owner = THIS_MODULE,
	.open = driver1_open,
	.release = driver1_close,
	.read = driver1_read,
	.write = driver1_write
	
};


 
static int __init driver1_init(void)
{
	
	int ret ;
	
	printk("%s,%d\r\n", __func__, __LINE__); 
	
	
	ret= register_chrdev(major_version,driver1_name, &ops);
	
	
	
	
	return ret;
}
 
static void __exit driver1_exit(void)
{
	printk("%s,%d\r\n", __func__, __LINE__);
	unregister_chrdev(major_version, driver1_name);
}
 
module_init(driver1_init);
module_exit(driver1_exit);

MODULE_LICENSE("GPL");

